import logging
from commons.conf import config
import os


class AppLogger:
    def __init__(self):
        logger_level = config.read_conf('default', 'logger_level')
        if logger_level is None:
            logger_level = 'INFO'

        logger_level = logger_level.upper()
        if logger_level == 'DEBUG':
            level = logging.DEBUG
        elif logger_level == 'INFO':
            level = logging.INFO
        elif logger_level == 'WARNING':
            level = logging.WARNING
        elif logger_level == 'ERROR':
            level = logging.ERROR
        elif logger_level == 'CRITICAL':
            level = logging.CRITICAL
        else:
            level = logging.INFO

        root_path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
        fn = os.path.join(root_path, 'log', 'process.log')
        logging.basicConfig(filename=fn, filemode="w",
                            format="%(asctime)s %(name)s:%(levelname)s:%(message)s",
                            datefmt="%d-%M-%Y %H:%M:%S", level=level)

    def info(self, msg, *args, **kwargs):
        logging.info(msg, *args, **kwargs)

    def error(self, msg, *args, **kwargs):
        logging.error(msg, *args, **kwargs)

    def debug(self, msg, *args, **kwargs):
        logging.debug(msg, *args, **kwargs)

    def critical(self, msg, *args, **kwargs):
        logging.critical(msg, *args, **kwargs)


logger = AppLogger()
